package com.project.UI;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import com.base.MyLog;
import com.example.mybaseproject.R;
import com.project.baseApp.BaseActivity;
import com.project.utils.HttpHelper;
import com.project.utils.Utils;
import com.support.universalimageloader.core.ImageLoader;
import com.support.universalimageloader.core.assist.FailReason;
import com.support.universalimageloader.core.assist.ImageLoadingListener;
import com.support.volley.RequestQueue;
import com.support.volley.Response.ErrorListener;
import com.support.volley.Response.Listener;
import com.support.volley.VolleyError;
import com.support.volley.toolbox.StringRequest;
import com.support.volley.toolbox.Volley;

public class StartActivity extends BaseActivity implements ImageLoadingListener {
	private ImageView IV_start;
	private int[] deviceInfo;

	@Override
	protected void onCreate(Bundle arg0) {
		deviceInfo = Utils.getDeviceInfo(this);
		super.onCreate(arg0);
		setContentView(R.layout.activity_start);
		IV_start = (ImageView) findViewById(R.id.start_iv);
		loadADData(deviceInfo[0], deviceInfo[1]);
	}

	private void loadADData(int... params) {
		RequestQueue rq = Volley.newRequestQueue(getApplicationContext());
		Listener<String> listener = new Listener<String>() {
			@Override
			public void onResponse(String response) {
				parseADData(response);
			}
		};
		ErrorListener errorListener = new ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError error) {
				toNextActivity();
			}
		};
		rq.add(new StringRequest(
				HttpHelper.creatAPIUrl("Portal/p_open_ad/width/" + params[0] + "/height/" + params[1]), listener,
				errorListener));
		rq.start();
	}

	@Override
	public void onLoadingStarted(String imageUri, View view) {

	}

	@Override
	public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
		toNextActivity();
	}

	@Override
	public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
		pause();
	}

	@Override
	public void onLoadingCancelled(String imageUri, View view) {
		toNextActivity();
	}

	private void toNextActivity() {
		Intent intent = new Intent(StartActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
	}

	private void parseADData(String result) {
		Intent intent = null;
		if (result == null) {
			toNextActivity();
		} else {
			try {
				JSONArray array = (new JSONObject(result)).getJSONArray("node");
				int adNum = Integer.parseInt(Utils.getPref(this, "adNum", "0"));
				if ((adNum + 1) < array.length()) {
					adNum = adNum + 1;
				} else {
					adNum = 0;
				}
				MyLog.d("ADNum", "length=" + array.length() + "\n currentNum" + adNum);
				Utils.savePref(this, "adNum", adNum + "");
				JSONObject obj = array.getJSONObject(adNum);
				ImageLoader.getInstance().displayImage(obj.getString("image"), IV_start, this);

			} catch (JSONException e) {
				toNextActivity();
				e.printStackTrace();
			}
		}
	}

	private void pause() {
		int pause = 2000;
		// if (isClicked) {
		// pause = 5000;
		// }
		handler.postDelayed(run, pause);
	}

	Handler handler = new Handler();
	Runnable run = new Runnable() {

		@Override
		public void run() {
			toNextActivity();
		}
	};
}
